package javabase.xml.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: luzhanshi
 * @Date: 2021/2/21 - 02 - 21 - 22:42 
 * @Description: javabase.xml.jsoup
 * @version: 1.0
 */
public class JsoupDemo4 {
    public static void main(String[] args) throws IOException {
        String path = JsoupDemo4.class.getClassLoader().getResource("student.xml").getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");
        //3.获取元素对象
        Elements names = document.getElementsByTag("name");
        System.out.println(names.size());//2
        //3.1获取第一个name的Element对象
        Element element = names.get(0);
        //3.2通过其键获取属性的值。
        String id = element.attr("id");
        System.out.println(id);//111
        //3.3获取元素标签内部的值
        String text = element.text();
        System.out.println(text);//tom
        Elements elementsByAttributeValue = document.getElementsByAttributeValue("number", "s1001");
        String text1 = elementsByAttributeValue.text();
        System.out.println(text1);//tom 18 1   将所有子元素的的值都获取
        String html = elementsByAttributeValue.html();
        System.out.println(html);//检索元素的内部HTML。
        /**
         * 控制台最终所有输出：
         * 2
         * 111
         * tom
         * tom 18 1
         * <name id="111">
         *  tom
         * </name>
         * <age>
         *  18
         * </age>
         * <sex>
         *  1
         * </sex>
         */
    }

}
